package com.hjx.other;

/**
 * 位运算
 */
public class BitOperator {

    public static void main(String[] args) {
//        xor();
//        not();
//        or();
//        and();

        shift();
    }

    public static void shift(){
        int a = -1;
        System.out.println(Integer.toBinaryString(a));
        System.out.println(Integer.toBinaryString(a << 2));
        System.out.println(Integer.toBinaryString(a << 3));
        System.out.println(Integer.toBinaryString(a >> 1));
        System.out.println(Integer.toBinaryString(a << 3 >>> 3));
    }

    public static void and(){
        int a = 1;
        int b = 2;
        System.out.println(Integer.toBinaryString(a & b));
        System.out.println(Integer.toBinaryString(b & b));
    }

    public static void or(){
        int a = 1;
        int b = 2;
        System.out.println(Integer.toBinaryString(a | b));
        System.out.println(Integer.toBinaryString(a ^ b));
    }

    public static void not(){
        int a = 1;
        System.out.println(Integer.toBinaryString(~a));
        System.out.println(Integer.toBinaryString(-2));
    }

    public static void xor(){
        //异或
        int a = 1;
        int b = 2;

        /*
         *  a   00000001
         * ^b   00000010
         *  a   00000011
         *
         *  a   00000011
         * ^b   00000010
         *  b   00000001
         *
         *  b   00000001
         * ^a   00000011
         *  a   00000010
         */
        a = a ^ b;
        b = a ^ b;
        a = b ^ a;


        System.out.println(a);
        System.out.println(b);

        System.out.println((a^b^b) == a);
        System.out.println((b^a^a) == b);
    }
}
